<?php

namespace Home\Controller;

class SettingController extends ParentController {

    /**
     * 申请短信通知视图页面
     * @modify songanwei 2016-03-23
     */
    public function notice() {
        $settingModel = D('Setting');
        $setting = $settingModel->getInfo(1);

        $this->assign('setting', $setting);
        $this->display();
    }

    /**
     * 设置打印二维码默认字符串页面
     * @modify songanwei 2016-03-23
     */
    public function printQrcode() {
        $settingModel = D('Setting');
        $setting = $settingModel->getInfo(1);

        $this->assign('setting', $setting);
        $this->display();
    }

    /**
     * 分销渠道视图页面
     * @modify trainyao 2016-05-20 创建
     */
    public function channel() {
        $channelModel = D('Channel');
        $allChannel = $channelModel->getAll();

        $this->assign('channel', $allChannel);
        $this->display();
    }

    /**
     * 支付方式视图页面
     * @modify trainyao 2016-05-20 创建
     */
    public function pay() {
        $payModel = D('Pay');
        $allPay = $payModel->getAll();

        $this->assign('pay', $allPay);
        $this->display();
    }

    /**
     * 更新申请通知手机号
     * @modify songanwei 2016-03-23
     */
    public function updateNotice() {
        $param  = I('post.');

        $mobile = isset($param['mobile']) ? $param['mobile'] : null;
        $defaultString = isset($param['defaultString']) ? $param['defaultString'] : null;

        //验证参数
        
        $settingModel = D('Setting');
        $result = $settingModel->saveInfo(1, $mobile, $defaultString);
        if ($result == false) {
            returnJson(403, 506);
            return;
        }

        returnJson(200, 200);
    }

    /**
     * 添加默认渠道
     * @modify trainyao 2016-05-19 创建
     */
    public function addChannel() {
        $name = I('post.name');
        if (is_null($name) || empty($name) || strlen($name) > 150 || strpos($name, '/') === false || strpos($name, '/') == strrpos($name, '/')) {
            $this->returnJson(403, 213);
            return;
        }

        // 查看渠道是否已经存在
        $channelModel = D('Channel');
        $allChannel = $channelModel->getAll();
        $allChannelName = array();
        if (!empty($allChannel)) {
            $allChannelName = array_column($allChannel, 'name');
        }
        if (in_array($name, $allChannelName)) {
            $this->returnJson(403, 300);
            return;
        }

        // 修改默认渠道
        $channelModel->startTrans();
        $commit = true;

        $addResult = $channelModel->addInfo($name);
        if (empty($addResult)) {
            $commit = false;
        }

        // 修改名字已存在的客栈支付方式
        $hotelChannelModel = D('HotelChannel');
        $hotelChannelInfo = $hotelChannelModel->getInfoByName($name);
        if (!empty($hotelChannelInfo)) {
            $hotelChannelInfoIds = array_column($hotelChannelInfo, 'id');
        }
        if ($commit && !empty($hotelChannelInfoIds)) {
            $addResult = $hotelChannelModel->saveType($hotelChannelInfoIds, ENABLE);
            if (empty($addResult)) {
                $commit = false;
            }
        }

        // 给没这个默认渠道的客栈赋予默认渠道
        $channelExistHotelIds = array_unique(array_column($hotelChannelInfo, 'hotelentity_id'));
        $hotelEntityModel = D('HotelEntity');
        $totalHotelIds = $hotelEntityModel->getAllId();
        $totalHotelIds = array_column($totalHotelIds, 'id');
        // 算出有哪些客栈没有赋予$name默认渠道
        $totalHotelIds = array_diff($totalHotelIds, $channelExistHotelIds);
        if (!empty($totalHotelIds)) {
            $addResult = $hotelChannelModel->addInfo($totalHotelIds, $name, ENABLE);
            if (empty($addResult)) {
                $commit = false;
            }
        }

        if ($commit) {
            $channelModel->commit();
            $this->returnJson(200, 200);
        } else {
            $channelModel->rollback();
            $this->returnJson(500, 508);
        }
    }

    /**
     * 删除一个默认渠道
     * @modify trainyao 2016-05-20    创建
     */
    public function deleteChannel() {
        $name = I('post.name');
        if (is_null($name) || empty($name) || strlen($name) > 150 || strpos($name, '/') === false || strpos($name, '/') == strrpos($name, '/')) {
            $this->returnJson(403, 213);
            return;
        }

        // 查看渠道是否已经存在
        $channelModel = D('Channel');
        $allChannel = $channelModel->getAll();
        $allChannelName = array();
        if (!empty($allChannel)) {
            $allChannelName = array_column($allChannel, 'name');
        }
        if (!in_array($name, $allChannelName)) {
            $this->returnJson(403, 301);
            return;
        }

        // 执行修改
        $channelModel->startTrans();
        $commit = true;

        // 删除默认渠道
        $deleteResult = $channelModel->deleteInfoByName($name);
        if (empty($deleteResult)) {
            $commt = false;
        }

        // 客栈与默认支付渠道改为可删除
        if ($commit) {
            $hotelChannelModel = D('HotelChannel');
            $hotelChannelInfo = $hotelChannelModel->getInfoByName($name);

            if (!empty($hotelChannelInfo)) {
                $hotelChannelInfoIds = array_column($hotelChannelInfo, 'id');
                $deleteResult = $hotelChannelModel->saveType($hotelChannelInfoIds, DISABLE);
                if (empty($deleteResult)) {
                    $commit = false;
                }
            }
        }

        if ($commit) {
            $channelModel->commit();
            $this->returnJson(200, 200);
        } else {
            $channelModel->rollback();
            $this->returnJson(500, 510);
        }
    }

    /**
     * 添加默认支付方式
     * @modify trainyao 2016-05-19 创建
     */
    public function addPay() {
        $name = I('post.name');
        if (is_null($name) || empty($name) || strlen($name) > 150 || strpos($name, '/') === false || strpos($name, '/') == strrpos($name, '/')) {
            $this->returnJson(403, 213);
            return;
        }

        // 查看支付方式是否已经存在
        $payModel = D('Pay');
        $allPay = $payModel->getAll();
        $allPayName = array();
        if (!empty($allPay)) {
            $allPayName = array_column($allPay, 'name');
        }
        if (in_array($name, $allPayName)) {
            $this->returnJson(403, 302);
            return;
        }

        // 修改默认支付方式
        $payModel->startTrans();
        $commit = true;

        $addResult = $payModel->addInfo($name);
        if (empty($addResult)) {
            $commit = false;
        }

        // 修改名字已存在的客栈支付方式
        $hotelPayModel = D('HotelPay');
        $hotelPayInfo = $hotelPayModel->getInfoByName($name);
        if (!empty($hotelPayInfo)) {
            $hotelPayInfoIds = array_column($hotelPayInfo, 'id');
        }
        if ($commit && !empty($hotelPayInfoIds)) {
            $addResult = $hotelPayModel->saveType($hotelPayInfoIds, ENABLE);
            if (empty($addResult)) {
                $commit = false;
            }
        }

        // 给没这个默认支付方式的客栈赋予默认支付方式
        $payExistHotelIds = array_unique(array_column($hotelPayInfo, 'hotelentity_id'));
        $hotelEntityModel = D('HotelEntity');
        $totalHotelIds = $hotelEntityModel->getAllId();
        $totalHotelIds = array_column($totalHotelIds, 'id');
        // 算出有哪些客栈没有赋予$name默认支付方式
        $totalHotelIds = array_diff($totalHotelIds, $payExistHotelIds);
        if (!empty($totalHotelIds)) {
            $addResult = $hotelPayModel->addInfo($totalHotelIds, $name, ENABLE);
            if (empty($addResult)) {
                $commit = false;
            }
        }

        if ($commit) {
            $payModel->commit();
            $this->returnJson(200, 200);
        } else {
            $payModel->rollback();
            $this->returnJson(500, 508);
        }
    }

    /**
     * 删除一个默认支付方式
     * @modify trainyao 2016-05-20    创建
     */
    public function deletePay() {
        $name = I('post.name');
        if (is_null($name) || empty($name) || strlen($name) > 150 || strpos($name, '/') === false || strpos($name, '/') == strrpos($name, '/')) {
            $this->returnJson(403, 213);
            return;
        }

        // 查看支付方式是否已经存在
        $payModel = D('Pay');
        $allPay = $payModel->getAll();
        $allPayName = array();
        if (!empty($allPay)) {
            $allPayName = array_column($allPay, 'name');
        }
        if (!in_array($name, $allPayName)) {
            $this->returnJson(403, 303);
            return;
        }

        // 执行修改
        $payModel->startTrans();
        $commit = true;

        // 删除默认支付方式
        $deleteResult = $payModel->deleteInfoByName($name);
        if (empty($deleteResult)) {
            $commt = false;
        }

        // 客栈与默认支付支付方式改为可删除
        if ($commit) {
            $hotelPayModel = D('HotelPay');
            $hotelPayInfo = $hotelPayModel->getInfoByName($name);

            if (!empty($hotelPayInfo)) {
                $hotelPayInfoIds = array_column($hotelPayInfo, 'id');
                $deleteResult = $hotelPayModel->saveType($hotelPayInfoIds, DISABLE);
                if (empty($deleteResult)) {
                    $commit = false;
                }
            }
        }

        if ($commit) {
            $payModel->commit();
            $this->returnJson(200, 200);
        } else {
            $payModel->rollback();
            $this->returnJson(500, 510);
        }
    }


}
